package mini.hug;

import static bogus.core.BogusCore.egl;

import java.awt.Point;
import java.util.ArrayList;

import audio.Audio;
import audio.Clip;
import owg.util.Calc;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import world.Sound;
import hubol.HMinigame;
import hubol.Sprite;

public class Hug extends HMinigame{
	public final static float SIZE = .25f;
	
	public static Hug me;
	public Shake[] s;
	
	public static final Sprite FLOWER = new Sprite("flower"), SPEECH = new Sprite("speech"), TEXT = new Sprite("text"), BORGUS = new Sprite("borgus"), HEART = new Sprite("heart"), EXHAUST = new Sprite("heart"), BIRGUS = new Sprite("birgus");
	
	private final Sprite background = new Sprite("background");
	private final Clip music = Audio.getClip("music");
	
	private double xDist = 0, yDist = 0;
	
	public Bogus bogus;
	public ArrayList<Borgus> borgus = new ArrayList<Borgus>();
	private ArrayList<Integer> claimed = new ArrayList<Integer>();
	
	public int SCORE = 0, ROUND = 0;

	@Override
	public void initImpl() {
		me = this;
		s = Shake.shake(16);
		HEART.setOrigin(Compass.SOUTH);
		BORGUS.setOrigin(Compass.SOUTH);
		
		new Title();
		
		viewY = 0;
	}
	
	public void stop(){
		music.stop();
	}
	
	public void pitch(double pitch){
		music.setSpeed(null, pitch);
	}
	
	public void gain(double gain){
		music.setPosition(null, gain);
	}
	
	public void reset(){
		pitch(1);
		gain(1);
		SCORE = 0;
		ROUND++;
		
		xDist = 0;
		yDist = 0;
		bogus = null;
		forceUpdate();
		
		borgus.clear();
		borgus = new ArrayList<Borgus>();
		claimed.clear();
		claimed = new ArrayList<Integer>();
		
		s = null;
		
		list.destroyAll();
		s = Shake.shake(16);
		generate();
	}
	
	public void addBogus(){
		bogus = new Bogus(432, 243);
	}
	
	public void addBorgus(){
		int message = (int)Calc.random(8);
		if (ROUND > 0){
			while (claimed.contains(message)){
				message++;
				if (message > 7)
					message = 0;
			}
			claimed.add(message);
		}
		else
			message = 3 + (borgus.size() % 5);
		double dir = 45 * borgus.size();
		borgus.add(new Borgus(432 + Calc.dirX(432, dir), 243 + Calc.dirY(243, dir), message));
		Sound.playPitched("growborgus", .254);
	}
	
	public Point getBogus(){
		if (bogus != null)
			return new Point((int)bogus.x, (int)bogus.y);
		return new Point(432, 243);
	}
	
	public boolean runNearby(){
		boolean out = false;
		if (SCORE < 3){
			Point p = getBogus();
			for (int i=0; i<borgus.size(); i++){
				Borgus b = borgus.get(i);
				if (!b.hug && !b.fuk){
					if (b.hugMask.contains(p))
						b.hug();
					else if (b.speakMask.contains(p)){
						if (!b.prev)
							Sound.playPitched("growborgus", .254);
						b.speak = true;
						out = true;
					}
				}
			}
		}
		
		return out;
	}
	
	public void play(){
		music.loop();
	}
	
	public void step(){
		super.step();
		Point p = getBogus();
		viewX = Calc.approach(viewX, p.x - 432, 8);
		viewY = Calc.approach(viewY, p.y - 243, 8);
		quitStep();
	}
	
	public void rememberDistance(){
		Point p = getBogus();
		xDist = viewX - (p.x - 432);
		yDist = viewY - (p.y - 243);
	}
	
	public void forceUpdate(){
		Point p = getBogus();
		viewX = p.x + s[0].x - 432 + xDist;
		viewY = p.y + s[0].y - 243 + yDist;
	}
	
	public void generate(){
		new Generate();
	}
	
	public void dispose(){
		super.dispose();
		me = null;
	}
	
	public Shake shake(){
		return s[(int)Calc.random(16)];
	}

	@Override
	public int getFps() {
		return 60;
	}
	
	public void render(){
		if (bogus != null && runNearby())
			bogus.hug = 1;

		egl.modelviewMatrix().identity();
    	egl.projectionMatrix().identity();
    	egl.projectionMatrix().ortho((float)viewX, width + (float)viewX, height + (float)viewY, (float)viewY, -1, 1);

    	egl.setColor(ColorF.WHITE);
    	egl.setAlpha(1f);
    	
    	background.render(0, 432 + s[0].x + viewX, 243 + s[0].y + viewY, .55f, .55f, (float)(s[0].z / 50.0), ColorF.WHITE);
    	
		if (list != null)
		    list.render();
		
		quitRender();
	}

	@Override
	public void completeMinigameImpl() {
		// TODO Auto-generated method stub
		
	}

}
